package com.losonensoundboard.losonensoundboard;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Main extends ListActivity {
    private ArrayList<Sound> mSounds = null;
    private SoundAdapter mAdapter = null;
    private static MediaPlayer mMediaPlayer = null;
    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        initControls();

        // create a list of sounds
        mSounds = new ArrayList<Sound>();

        Sound s = new Sound();
        s.setDescription(getString(R.string.sound01));
        s.setSoundResourceId(R.raw.sound01);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound02));
        s.setSoundResourceId(R.raw.sound02);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound03));
        s.setSoundResourceId(R.raw.sound03);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound04));
        s.setSoundResourceId(R.raw.sound04);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound05));
        s.setSoundResourceId(R.raw.sound05);
        s.setIconResourceId(R.drawable.tn_rmj);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound07));
        s.setSoundResourceId(R.raw.sound07);
        s.setIconResourceId(R.drawable.tn_rmj);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound08));
        s.setSoundResourceId(R.raw.sound08);
        s.setIconResourceId(R.drawable.tn_rmj);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound09));
        s.setSoundResourceId(R.raw.sound09);
        s.setIconResourceId(R.drawable.tn_rmj);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound06));
        s.setSoundResourceId(R.raw.sound06);
        s.setIconResourceId(R.drawable.tn_rmj);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound10));
        s.setSoundResourceId(R.raw.sound10);
        s.setIconResourceId(R.drawable.tn_rmj);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound11));
        s.setSoundResourceId(R.raw.sound11);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound12));
        s.setSoundResourceId(R.raw.sound12);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound13));
        s.setSoundResourceId(R.raw.sound13);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound14));
        s.setSoundResourceId(R.raw.sound14);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound15));
        s.setSoundResourceId(R.raw.sound15);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound16));
        s.setSoundResourceId(R.raw.sound16);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound17));
        s.setSoundResourceId(R.raw.sound17);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound18));
        s.setSoundResourceId(R.raw.sound18);
        s.setIconResourceId(R.drawable.tn_poliisit);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound19));
        s.setSoundResourceId(R.raw.sound19);
        s.setIconResourceId(R.drawable.tn_poliisit);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound20));
        s.setSoundResourceId(R.raw.sound20);
        s.setIconResourceId(R.drawable.tn_poliisit);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound21));
        s.setSoundResourceId(R.raw.sound21);
        s.setIconResourceId(R.drawable.tn_poliisit);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound22));
        s.setSoundResourceId(R.raw.sound22);
        s.setIconResourceId(R.drawable.tn_poliisit);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound23));
        s.setSoundResourceId(R.raw.sound23);
        s.setIconResourceId(R.drawable.tn_poliisit);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound24));
        s.setSoundResourceId(R.raw.sound24);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound25));
        s.setSoundResourceId(R.raw.sound25);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound26));
        s.setSoundResourceId(R.raw.sound26);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound27));
        s.setSoundResourceId(R.raw.sound27);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound28));
        s.setSoundResourceId(R.raw.sound28);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound29));
        s.setSoundResourceId(R.raw.sound29);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound30));
        s.setSoundResourceId(R.raw.sound30);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound31));
        s.setSoundResourceId(R.raw.sound31);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound32));
        s.setSoundResourceId(R.raw.sound32);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound33));
        s.setSoundResourceId(R.raw.sound33);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound34));
        s.setSoundResourceId(R.raw.sound34);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound36));
        s.setSoundResourceId(R.raw.sound36);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound37));
        s.setSoundResourceId(R.raw.sound37);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound38));
        s.setSoundResourceId(R.raw.sound38);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound35));
        s.setSoundResourceId(R.raw.sound35);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound39));
        s.setSoundResourceId(R.raw.sound39);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound40));
        s.setSoundResourceId(R.raw.sound40);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound41));
        s.setSoundResourceId(R.raw.sound41);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound42));
        s.setSoundResourceId(R.raw.sound42);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound43));
        s.setSoundResourceId(R.raw.sound43);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sound44));
        s.setSoundResourceId(R.raw.sound44);
        s.setIconResourceId(R.drawable.tn_mp4);
        mSounds.add(s);

        mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
        setListAdapter(mAdapter);
        getListView().setDivider(null);
        getListView().setDividerHeight(0);

    }

    private void initControls() {
        try {
            volumeSeekbar = (SeekBar) findViewById(R.id.volume_control);
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            volumeSeekbar.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volumeSeekbar.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));

            volumeSeekbar
                    .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                        @Override
                        public void onStopTrackingTouch(SeekBar arg0) {
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar arg0) {
                        }

                        @Override
                        public void onProgressChanged(SeekBar arg0,
                                int progress, boolean arg2) {
                            audioManager.setStreamVolume(
                                    AudioManager.STREAM_MUSIC, progress, 0);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onListItemClick(ListView parent, View v, int position, long id) {
        initControls();
        Sound s = mSounds.get(position);
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
        }
        mMediaPlayer = MediaPlayer.create(this, s.getSoundResourceId());
        mMediaPlayer.start();
    }
}